package com.itheima.homework.test;

import java.util.ArrayList;

/*
某公司发放年终礼品，但hr在统计的时候发现有很多同姓名同年龄的员工，所以决定对这一部分员工，
发放【缘分大礼包】，其余员工发放【普通大礼包】，请用技术手段，完成礼品的分发。 现有4个员工信息：
张三，23
李四，24
张三，23
张三，28
运行结果：
Employee{name=张三,age=23}获得了缘分大礼包
Employee{name=李四,age=24}获得了普通大礼包
Employee{name=张三,age=23}获得了缘分大礼包
Employee{name=张三,age=28}获得了普通大礼包
 */
public class Demo4 {
    //用集合装元素
    //对于同属性的发放缘分大礼包,对于不同属性发放普通大礼包
    //利用equals来比对如果是true就发缘分大礼包,false就发普通大礼包

    public static void main(String[] args) {
        Employee e1 = new Employee("张三",23);
        Employee e2 = new Employee("李四",24);
        Employee e3 = new Employee("张三",23);
        Employee e4 = new Employee("张三",28);

        ArrayList<Employee> list = new ArrayList<>();
        list.add(e1);
        list.add(e2);
        list.add(e3);
        list.add(e4);


        for (int i = 0; i < list.size(); i++) {
            Employee emp = list.get(i);
            //标记位思想很重要
            boolean flag = false;
            for (int j = 0; j < list.size(); j++) {
                Employee emp1 = list.get(j);
                if (emp != emp1 && emp.equals(emp1)){
                    flag = true;
                }
            }

            if (flag){
                System.out.println(emp.toString()+"获得了缘分大礼包");
            }else {
                System.out.println(emp.toString()+"获得了普通大礼包");
            }
        }

    }
    }

    //定义一个员工类
     class Employee {
        private String name;
        private int age;

        public Employee() {
        }

        public Employee(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public  void setAge(int age) {
            this.age = age;
        }

        @Override
        public String toString() {
            return "Employee{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Employee employee = (Employee) o;

            if (age != employee.age) return false;
            return name != null ? name.equals(employee.name) : employee.name == null;
        }

    }

